package com.tianfei.crowd.mvc.controller;

import com.github.pagehelper.PageInfo;
import com.tianfei.crowd.constant.CrowdConstants;
import com.tianfei.crowd.entity.Admin;
import com.tianfei.crowd.service.api.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;

/**
 * @author: Herz
 * @date: 2021/7/6 18:24
 */
@SuppressWarnings("all")
@Controller
public class AdminController {

    @Autowired
    private AdminService adminService;

    @RequestMapping("/admin-edit.html")
    public String editAdmin(Admin admin,@RequestParam("pageNum") Integer pageNum,@RequestParam("keyword") String keyword){

        adminService.update(admin);

        return "redirect:/admin-to-user-page.html?pageNum=" + pageNum + "&keyword=" + keyword;
    }



    @PreAuthorize("hasAuthority('user:save')")
    @RequestMapping("/admin-save.html")
    public String saveAdmin(Admin admin) {

        adminService.saveAdmin(admin);

        return "redirect:/admin-to-user-page.html?pageNum=" + Integer.MAX_VALUE;
    }


    @RequestMapping("/admin-edit-page.html")
    public String toEditPage(@RequestParam("adminId") Integer adminId, ModelMap modelMap) {

        // 根据adminId 查询得到 其所有信息
        Admin admin = adminService.getAdminById(adminId);

        // 存入到 modelMap 中
        modelMap.addAttribute("admin", admin);

        return "admin/admin-edit";
    }

    @RequestMapping("/admin-do-remove.html")
    public String removeAdmin(
            @RequestParam("adminId") Integer adminId,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("keyword") String keyword,
            HttpSession session
    ) {
        // 执行删除
        adminService.removeAdminById(adminId);

        // 返回分页页面
        // 重定向到/admin-to-user-page.html，为保持原页面信息，要带上pageNum和keyword
        return "redirect:/admin-to-user-page.html?pageNum=" + pageNum + "&keyword=" + keyword;
    }


    @RequestMapping("/admin-to-user-page.html")
    public String getPageInfo(
            @RequestParam(value = "keyword", defaultValue = "") String keyword,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            ModelMap modelMap
    ) {

        PageInfo<Admin> pageInfo = adminService.getPageInfo(keyword, pageNum, pageSize);

        modelMap.addAttribute(CrowdConstants.MESSAGE_NAME_PAGE_INFO, pageInfo);

        return "admin/admin-page";
    }



    // 未使用SpringSecurity 版 的退出请求
//    @RequestMapping("/admin-do-logout-page.html")
//    public String doLogout(HttpSession session) {
//
//        //session强制失效
//        session.invalidate();
//
//        return "redirect:/admin-login-to-page.html";
//    }


    // 未使用SpringSecurity 版 的登录请求
//    @RequestMapping("/admin-do-login-page.html")
//    public String doLogin(@RequestParam("login_acct") String loginAcct,
//                          @RequestParam("login_password") String loginPwd,
//                          HttpServletRequest request,
//                          HttpSession session) throws UnsupportedEncodingException {
//
//        // 调用Service中得方法进行登录检查
//        // 如果能够返回admin对象说明登录成功，如果账号、密码不正确则会抛出异常
//        Admin admin = adminService.getByLoginAccount(loginAcct, loginPwd);
//
//
//        // 将登录成功返回的admin对象存入Session域中
//        session.setAttribute(CrowdConstants.MESSAGE_ATTR_LOGIN, admin);
//        return "redirect:/admin-main-to-page.html";
//    }
}
